<?php 
/**
 * 优惠卷类型程序
 * 编写人    陈泽富
*/

define('IN_ECS', true);
require(dirname(__FILE__) . '/includes/init.php');
// /* act操作项的初始化 */
if (empty($_REQUEST['act']))
{
    $act = 'list';
}
else
{
    $act = trim($_REQUEST['act']);
}

if ($act == 'list') {
    $smarty->assign('ur_here',          $_LANG['11_coupon_type']);
    $smarty->assign('full_page',        1);
    $sql = "SELECT * FROM ".$ecs->table('coupon_type');
    $coupon_type =$db->getAll($sql);
    $smarty->assign('coupon_type', $coupon_type);
    $smarty->assign('action_link',      array('text' => '添加优惠类型', 'href' => 'coupon_type.php?act=add'));
    $smarty->display('coupon/coupon_type.htm');
}

if ($act == 'query') {
    $sql = "SELECT * FROM ".$ecs->table('coupon_type');
    $coupon_type =$db->getAll($sql);
    $smarty->assign('coupon_type', $coupon_type);

    make_json_result($smarty->fetch('coupon/coupon_type.htm'));
}






/*添加优惠卷类型列表*/
if ($act == 'add') {
    $smarty->assign('ur_here',          "新建优惠卷类型");
    $smarty->assign('full_page',        1);
    $smarty->assign('action_link',      array('text' => '优惠类型列表', 'href' => 'coupon_type.php?act=list'));
    $smarty->display('coupon/coupon_type_info.htm');
}

if ($act == 'edit') {
    $smarty->assign('ur_here',          "修改优惠类型");
    $smarty->assign('full_page',        1);
    $type_id = intval($_GET['type_id']);
    $sql = "SELECT * FROM ".$ecs->table('coupon_type')." WHERE type_id ='$type_id'";
    $coupon_info = $db->getRow($sql);
    $smarty->assign('coupon_info',$coupon_info);
    $smarty->assign('action_link',      array('text' => '优惠类型列表', 'href' => 'coupon_type.php?act=list'));
    $smarty->display('coupon/coupon_type_info.htm');
}









/*处理添加优惠卷类型数据*/
if ($act == 'post') {
    $type['type_name']   = !empty($_REQUEST['type_name'])?$_REQUEST['type_name']:'';
    $type['type_code']   = !empty($_REQUEST['type_code'])?$_REQUEST['type_code']:'';
    $type['is_free']     = !empty($_REQUEST['is_free'])?$_REQUEST['is_free']:'';
    $type['discount']    = !empty($_REQUEST['discount'])?$_REQUEST['discount']:'0';
    $type['coupon_note'] = !empty($_REQUEST['coupon_note'])?$_REQUEST['coupon_note']:'0';
    $type_id = !empty($_REQUEST['type_id'])?$_REQUEST['type_id']:'';
    if (empty($type_id)) {
        $db->autoExecute($ecs->table('coupon_type'), $type);
        if ($db->insert_id()) {
        $links[] = array('href' => 'coupon_type.php?act=list', 'text' => "添加类型成功");
        sys_msg("添加类型成功", 100, $links);
        }
    }else{
        $db->autoExecute($ecs->table('coupon_type'), $type,'UPDATE',"type_id = '$type_id'");
        $links[] = array('href' => 'coupon_type.php?act=list', 'text' => "修改类型成功");
        sys_msg("修改类型成功", 100, $links);
    }

}
/*删除优惠卷类型*/
if ($act == 'delete') {
    $id = intval($_GET['id']);
    require(ROOT_PATH . 'includes/cls_json.php');
    $json   = new JSON;
    $return = array('error' => '0','message' =>'成功' );
    /*查询能否删除*/
    $sql = "SELECT count(*) FROM ".$ecs->table('coupon')."WHERE type_id = '$id'";
    $is_delete = $db->getOne($sql);

    if (!empty($is_delete)) {
        $return = array('error' => '2','message' =>'该类型中存在优惠卷不能删除' );
        echo $json->encode($return);
        exit();
    }else{
        /*删除类型*/
        $sql = "DELETE FROM ".$ecs->table('coupon_type')." WHERE type_id = '$id'";
        $db->query($sql);
        echo $json->encode($return);
        exit();
    }






}











 ?>